/**
 * 自定义函数
 * */


//列表
function getList(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var sort_id = getParamByUrl('sort_id');
    var depart_id = getParamByUrl('depart_id');
    var staff_id = getParamByUrl('staff_id');
    var year_id = getParamByUrl('year_id');
    var month_id = getParamByUrl('month_id');
    if (depart_id==0 || depart_id == '') {
        depart_id = $('#depart_id').val();
    }
    if (staff_id==0 || staff_id == '') {
        staff_id = $('#staff_id').val();
    }
    if (year_id==0 || year_id == '') {
        year_id = $('#year_id').val();
    }
    if (month_id==0 || month_id == '') {
        month_id = $('#month_id').val();
    }
    if (sort_id==0 || sort_id == '') {
        sort_id = $('#sort_id').val();
    }
    var formData = {
        start: pageStart, limit: pages.limit,
        uid: staff_id, depart_id: depart_id,
        year_id: year_id, month_id: month_id, sort_id: sort_id,
        urole_id: uinfo.role_id, ustaff_id: uinfo.uid,
        udepart_id: uinfo.depart_id,
        is_leader: uinfo.is_leader,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'tongji/person',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.start = pageStart;
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    var xuhao, pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    h += '';
    for (i = 0; i < d.length; i ++) {
        xuhao = (pageStart - 1) * pages.limit + i + 1;
        h += '<tr class="text-c">';
        h += '<td title="' + d[i].id + '">' + xuhao + '</td>';
        h += '<td>' + d[i].in_day + '</td>';
        h += '<td>' + d[i].depart_name + '</td>';
        h += '<td>' + d[i].uname + '</td>';
        h += '<td>' + d[i].item_name + '</td>';
        // h += '<td>' + d[i].wh_name + '</td>';
        h += '<td>' + d[i].delay + '</td>';
        h += '</tr>';
    }
    return h;
}


// 跳转
function toJump(){
    var depart_id = $('#depart_id').val();
    var staff_id = $('#staff_id').val();
    var year_id = $('#year_id').val();
    var month_id = $('#month_id').val();
    var sort_id = $('#sort_id').val();
    var p = '?depart_id=' + depart_id;
    p += '&staff_id=' + staff_id;
    p += '&year_id=' + year_id;
    p += '&month_id=' + month_id;
    p += '&sort_id=' + sort_id;
    window.location.href = 'person.html' + p;
}


//选项
function getOption(){
    var uinfo = getAdminInfo();
    console.log(uinfo);
    if (!uinfo) { return }
    var departDiv = $('#depart_id');
    var staffDiv = $('#staff_id');
    if (uinfo.role_id < 3 || uinfo.role_id == 5 || uinfo.role_id == 6) {
        departDiv.show();
        staffDiv.show();
    }
    else if (uinfo.is_leader==1 || uinfo.role_id==3) {
        staffDiv.show();
    }
    else if (uinfo.is_leader == 0) {
    }
    var sortDiv = $('#sort_id'), sh = '';
    if (sort_id == 2) {
        sh += '<option value="1">按日期正序</option>';
        sh += '<option value="2" selected>按日期倒序</option>';
    } else {
        sh += '<option value="1" selected>按日期正序</option>';
        sh += '<option value="2">按日期倒序</option>';
    }
    sortDiv.html(sh);
    var formData = {
        uid: uinfo.uid,
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
    };
    if (depart_id > 0) {
        formData.depart_id = depart_id;
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'tongji/option',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                return
            }
            var staff = res.data.staff;
            var depart = res.data.depart;
            var h = '', i;
            //部门
            if (depart_id == 0) {
                h = '<option value="0" selected>-选择部门-</option>';
            } else {
                h = '<option value="0">-选择部门-</option>';
            }
            for (i = 0; i < depart.length; i ++) {
                if (depart_id == depart[i].id) {
                    h += '<option value="' + depart[i].id + '" selected>';
                } else {
                    h += '<option value="' + depart[i].id + '">';
                }
                h += depart[i].title;
                h += '</option>';
            }
            // $('#depart_id').html(h);
            departDiv.html(h);
            //员工
            if (staff_id == 0) {
                h = '<option value="0" selected>-选择员工-</option>';
            } else {
                h = '<option value="0">-选择员工-</option>';
            }
            for (i = 0; i < staff.length; i ++) {
                if (staff_id == staff[i].id) {
                    h += '<option value="' + staff[i].id + '" selected>';
                } else {
                    h += '<option value="' + staff[i].id + '">';
                }
                h += staff[i].title;
                h += '</option>';
            }
            // $('#staff_id').html(h);
            staffDiv.html(h);
            //年份
            // if (year_id == 0) {
            //     h = '<option value="0" selected>选择</option>';
            // } else {
            //     h = '<option value="0">选择</option>';
            // }
            h = '';
            for (i = 1980; i <= 2100; i ++) {
                if (year_id == i) {
                    h += '<option value="' + i + '" selected>';
                } else {
                    h += '<option value="' + i + '">';
                }
                h += i;
                h += '</option>';
            }
            $('#year_id').html(h);
            //月份
            // if (month_id == 0) {
            //     h = '<option value="0" selected>选择</option>';
            // } else {
            //     h = '<option value="0">选择</option>';
            // }
            h = '';
            for (i = 1; i <= 12; i ++) {
                if (month_id == i) {
                    h += '<option value="' + i + '" selected>';
                } else {
                    h += '<option value="' + i + '">';
                }
                h += i;
                h += '</option>';
            }
            $('#month_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//导出
function getOut(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var depart_id = getParamByUrl('depart_id');
    var staff_id = getParamByUrl('staff_id');
    var year_id = getParamByUrl('year_id');
    var month_id = getParamByUrl('month_id');
    var p = '?urole_id=' + uinfo.role_id;
    p += '&udepart_id=' + uinfo.depart_id;
    p += '&ustaff_id=' + uinfo.uid;
    p += '&year_id=' + year_id;
    p += '&month_id=' + month_id;
    p += '&uid=' + staff_id;
    p += '&depart_id=' + depart_id;
    p += '&year_id=' + year_id;
    p += '&month_id=' + month_id;
    window.location.href = '/api/admin/tongji/staff-month-out' + p;
}


// //导出
// function getOut(){
//     var depart = $('#depart_id').val();
//     var staff = $('#staff_id').val();
//     var year = $('#year_id').val();
//     var month = $('#month_id').val();
//     var uinfo = getAdminInfo();
//     // if (depart == 0) {
//     //     msgPop({msg: '部门必须的'}); return
//     // }
//     // if (month == 0) {
//     //     msgPop({msg: '月份必须的'}); return
//     // }
//     var p = '?depart_id=' + depart;
//     p += '&staff_id=' + staff;
//     p += '&year=' + year;
//     p += '&month=' + month;
//     p += '&uid=' + uinfo.uid;
//     window.location.href = '/api/admin/tongji/staff-month-out' + p;
// }